package cn.cagurzhan;

import cn.cagurzhan.domain.dto.CommandResultDto;
import cn.cagurzhan.domain.dto.DeviceShadowDto;
import cn.cagurzhan.service.IoTService;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.huaweicloud.sdk.core.auth.AbstractCredentials;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.*;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import com.huaweicloud.sdk.vpc.v3.VpcClient;
import com.huaweicloud.sdk.vpc.v3.model.ListVpcsRequest;
import com.huaweicloud.sdk.vpc.v3.model.ListVpcsResponse;
import com.huaweicloud.sdk.vpc.v3.region.VpcRegion;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

/**
 * @author AjaxZhan
 */
@SpringBootTest
public class TestIoT {
    private static final String REGION = "cn-north-4";

    @Autowired
    private IoTService ioTService;

    // @Test
    // public void testService(){
    //     IoTDAClient client = ioTService.initClient();
    //     Map<String,Object> params = new HashMap<>();
    //     params.put("window_angle",90);
    //     CommandResultDto commandResultDto = ioTService.sendCommand(client, DEVICE_ID, "machine_control", "window_control", params);
    //     if(ObjectUtil.isNotNull(commandResultDto)){
    //         System.out.println(JSON.toJSONString(commandResultDto));
    //     }
    // }


    // @Test
    // public void IoT(){
    //     // 配置认证信息
    //     // 可通过环境变量等方式配置认证信息，参考2.4认证信息管理章节
    //     ICredential auth = new BasicCredentials()
    //             .withAk(AK)
    //             .withSk(SK);

    //     // 创建服务客户端
    //     VpcClient client = VpcClient.newBuilder()
    //             .withCredential(auth)
    //             .withRegion(VpcRegion.valueOf(REGION))
    //             .build();

    //     // 发送请求并获取响应
    //     ListVpcsRequest request = new ListVpcsRequest();
    //     try {
    //         ListVpcsResponse response = client.listVpcs(request);
    //         System.out.println(response.toString());
    //     } catch (ConnectionException | RequestTimeoutException e) {
    //         e.printStackTrace();
    //     } catch (ServiceResponseException e) {
    //         e.printStackTrace();
    //         System.out.println(e.getHttpStatusCode());
    //         System.out.println(e.getRequestId());
    //         System.out.println(e.getErrorCode());
    //         System.out.println(e.getErrorMsg());
    //     }
    // }

    // @Test
    // public void testLogin(){

    //     ICredential auth = new BasicCredentials()
    //             .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
    //             .withAk(AK)
    //             .withSk(SK);

    //     IoTDAClient client = IoTDAClient.newBuilder()
    //             .withCredential(auth)
    //             .withRegion(IoTDARegion.valueOf(REGION))
    //             .build();
    //     ShowDeviceShadowRequest request = new ShowDeviceShadowRequest();
    //     request.setDeviceId(DEVICE_ID);
    //     try {
    //         ShowDeviceShadowResponse response = client.showDeviceShadow(request);
    //         System.out.println(JSON.toJSONString(response));
    //         System.out.println(response);
    //         DeviceShadowDto deviceShadowDto = JSON.parseObject(JSON.toJSONString(response), DeviceShadowDto.class);
    //         System.out.println(deviceShadowDto.toString());
    //         Map<String,Object> log = (Map<String,Object> ) deviceShadowDto.getShadow().get(0).getReported().get("properties");
    //         System.out.println("测试：");
    //         System.out.println(log);
    //     } catch (ConnectionException e) {
    //         e.printStackTrace();
    //     } catch (RequestTimeoutException e) {
    //         e.printStackTrace();
    //     } catch (ServiceResponseException e) {
    //         e.printStackTrace();
    //         System.out.println(e.getHttpStatusCode());
    //         System.out.println(e.getRequestId());
    //         System.out.println(e.getErrorCode());
    //         System.out.println(e.getErrorMsg());
    //     }
    // }

    // @Test
    // public void testCommand(){
    //     ICredential auth = new BasicCredentials()
    //             .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
    //             .withAk(AK)
    //             .withSk(SK);

    //     IoTDAClient client = IoTDAClient.newBuilder()
    //             .withCredential(auth)
    //             .withRegion(IoTDARegion.valueOf(REGION))
    //             .build();


    //     CreateCommandRequest request = new CreateCommandRequest();
    //     request.setDeviceId(DEVICE_ID);
    //     DeviceCommandRequest body = new DeviceCommandRequest();
    //     body.withCommandName("window_control");
    //     body.withParas("{\"window_angle\":\"90\"}");
    //     body.withServiceId("reboot");
    //     body.withServiceId("machine_control");
    //     request.withBody(body);
    //     try {
    //         CreateCommandResponse response = client.createCommand(request);
    //         System.out.println(JSON.toJSONString(response));
    //     } catch (ConnectionException e) {
    //         e.printStackTrace();
    //     } catch (RequestTimeoutException e) {
    //         e.printStackTrace();
    //     } catch (ServiceResponseException e) {
    //         e.printStackTrace();
    //         System.out.println(e.getHttpStatusCode());
    //         System.out.println(e.getRequestId());
    //         System.out.println(e.getErrorCode());
    //         System.out.println(e.getErrorMsg());
    //     }
    // }
}
